home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / charmap / source / helpwin.c < prev    next >
C/C++ Source or Header  |  1999-11-30  |  4KB  |  142 lines

  1. /***************************************************************
  2. ** helpwin.c: Affiche une fenêtre d'aide sur le caractère     **
  3. **            Écrit par T.Pierron                             **
  4. **            15-03-1999                                      **
  5. ***************************************************************/
  6.  
  7. #include <Intuition/Intuition.H>
  8. #include <Graphics/Rastport.H>
  9. #include <Graphics/Gfxmacros.H>
  10. #include <Exec/Memory.H>
  11.  
  12. #define    CATCOMP_STRINGS
  13. #include    "cmap_strings.h"
  14.  
  15. WORD I;
  16.  
  17. /**** Cette fonction, extrêmement importante, permet d'allouer    *****
  18. ***** un nouvel affichage sans être obligé d'ouvrir une fenê-    *****
  19. ***** tre, avec les lourdeurs qui suivent.                            ****/
  20. struct RastPort *Alloc_rastport( WORD larg,WORD haut,WORD depth )
  21. {
  22.     static struct RastPort *RP;
  23.     static struct BitMap *BM;
  24.     static Point *P;
  25.  
  26.     /* Alloue les trois structures: */
  27.     if( (RP=(void *) AllocMem(sizeof(struct tPoint)+sizeof(struct RastPort)+sizeof(struct BitMap),MEMF_PUBLIC))==NULL )
  28.         return NULL;
  29.  
  30.     /* Sauvegarde quelques données: */
  31.     BM = (struct BitMap *) (RP+1);
  32.     P  = (Point *) (BM+1);
  33.     RP->BitMap = BM; P->x = larg; P->y = haut;
  34.  
  35.     /* Prépare le BitMap: */
  36.     InitBitMap( BM, depth, larg, haut );
  37.  
  38.     /* Alloue la mémoire pour le Raster: */ 
  39.     for( I = 0; I < depth; I++ )
  40.     {
  41.         if( (BM->Planes[ I ] = (PLANEPTR) AllocRaster( larg, haut ))==NULL )
  42.             return NULL;
  43.  
  44.         /* Efface la mémoire avec l'aide du Blitter: */
  45.         BltClear( BM->Planes[ I ], RASSIZE( larg, haut ), 0 );
  46.     }
  47.  
  48.     /* Prépare le RastPort, en donnant un pointeur sur le BitMap. */
  49.     InitRastPort( RP );
  50.     RP->BitMap = BM;
  51.     return RP;
  52. }
  53.  
  54. static struct RastPort *RPW=NULL;
  55. extern struct RastPort *RP,RPT;
  56.  
  57. /**** Libère l'affichage alloué avec la fonction ci-dessus: ****/
  58. void Free_helpwin()
  59. {
  60.     register struct BitMap *BM;
  61.     register Point *P;
  62.     register WORD depth;
  63.  
  64.     if(RPW) {
  65.         /* Libère les bitplans un à un: */
  66.         for( I = 0,BM = RPW->BitMap,depth = BM->Depth,P = (Point *)(BM+1); I < depth; I++ )
  67.             if( BM->Planes[ I ] )
  68.                 FreeRaster( BM->Planes[ I ], P->x, P->y );
  69.  
  70.         /* Et la mémoire pour les structures de données: */
  71.         FreeMem(RPW,sizeof(struct tPoint)+sizeof(struct RastPort)+sizeof(struct BitMap));
  72.         RPW=0;
  73.     }
  74. }
  75.  
  76. UBYTE *popmsg[]={ MSG_CODE_STR,MSG_DEC_STR,MSG_HEX_STR,MSG_OCT_STR,0 };
  77. UBYTE *NoMem   =MSG_NOMEM_STR;
  78. static WORD WinW,WinH,Larg,X,Y;
  79. extern UBYTE Font_height;
  80.  
  81. /**** Calcule la largueur et la hauteur de la fenêtre en fonction de la police: ****/
  82. void Init_helpwin()
  83. {
  84.     extern struct Screen *screen;
  85.     register UBYTE **p;
  86.  
  87.     for(p=popmsg,Larg=0,WinH=3; *p; p++,WinH += Font_height-3)
  88.         if(Larg < (WinW=TextLength(&RPT,*p,strlen(*p)))) Larg = WinW;
  89.  
  90.     Larg += 10; WinW = Larg+5+TextLength(&RPT,"999",3); /* WinH -= 9; */
  91.     if( !(RPW=Alloc_rastport(WinW,WinH,screen->BitMap.Depth)) )
  92.         cleanup(NoMem,30);
  93. }
  94.  
  95. /**** Conversion binaire => Base ****/
  96. void convert_to_base(UBYTE *Str,UBYTE Var,BYTE Base)
  97. {
  98.     UBYTE ChConv[]="0123456789ABCDEF";
  99.     UBYTE *ptr=Str,L=0;
  100.  
  101.     Var &= 0xFF;
  102.  
  103.     do {
  104.         ptr--; L++;
  105.         *ptr = ChConv[ Var%Base ];
  106.     } while(Var /= Base);
  107.  
  108.     Text(RP,ptr,L);
  109. }
  110.  
  111. /**** Affiche toute les infos de la fenêtre: ****/
  112. void Open_helpwin(UBYTE Num,WORD x,WORD y)
  113. {
  114.     extern struct Window *window;
  115.     extern WORD      txtpen,filltxt;
  116.     static UBYTE Str[6];
  117.     int i;
  118.  
  119.     if(x+WinW>window->Width)  x=window->Width-WinW;
  120.     if(y+WinH>window->Height) y=window->Height-WinH;
  121.  
  122.     /* Sauvegarde l'affichage de la fenêtre: */
  123.     ClipBlit(RP,x,y,RPW,0,0,WinW,WinH,0xC0);
  124.  
  125.     SetOPen(RP,txtpen); SetAPen(RP,filltxt);
  126.     RectFill(RP,x,y,x+WinW-1,y+WinH-1);
  127.     BNDRYOFF(RP);  SetAPen(RP,txtpen);
  128.  
  129.     X=x; Y=y;
  130.     for(i=0,y+=Font_height-4; i<sizeof(popmsg)/sizeof(UBYTE *)-1; i++,y+=Font_height-3)
  131.     {
  132.         Move(RP,x+5,y);    Text(RP,popmsg[i],strlen(popmsg[i]));    Move(RP,x+Larg,RP->cp_y);
  133.         if(i==0)    Text(RP,&Num,1);
  134.         else convert_to_base(Str+6,Num,i==1? 10 : i==2? 16 : 8);
  135.     }
  136. }
  137.  
  138. void Close_helpwin()
  139. {
  140.     ClipBlit(RPW,0,0,RP,X,Y,WinW,WinH,0xC0);
  141. }
  142.